package com.rmuti.programming.plugin.cplugin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import com.rmuti.programming.plugin.AbstractHandlePlugin;

public class HandleCPlugin extends AbstractHandlePlugin {

	private static String currentDir = System.getProperty("user.dir");
	private static String clabDir = currentDir + "\\clab";

	public HandleCPlugin() {
	}

	public void createTempFolder() {
		File f = new File(clabDir);
		f.mkdir();
	}

	public void removeTempFolder() {

	}

	public void copyStudentSourceToTemp() {
		File source = new File(
				"C:\\eclipse\\clab-plugin-dev\\eclipse\\runtime-EclipseApplication\\lab 1 - 1\\main.c");
		File dest = new File(clabDir + "\\main.c");
		try {
			Files.copy(source.toPath(), dest.toPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void createJNISourceFile() {
		templateWriteFile(clabDir + "\\call_java.c",JNICreator.createSourceFile());
	}

	private void createJNIHeaderFile() {
		templateWriteFile(clabDir+ "\\com_rmuti_programming_callc_CallCModule.h", JNICreator.createHeaderFile());
	}
	
	private void createMakefile(){
		templateWriteFile(clabDir+"\\Makefile", JNICreator.createMakefile());
//		File f = new File(clabDir+"\\Makefile");
//		try {
//			FileWriter fw = new FileWriter(f);
//			fw.write("# Compiler command \n");
//			fw.write("CC = C:\\MinGW\\bin\\mingw32-gcc \n");
//			fw.write("dllpro = C:\\MinGW\\bin\\dllwrap \n");
//			fw.write("main: main.o \n");
//			fw.write("	$(CC) -c main.c call_java.c -I \"C:\\Program Files\\Java\\jdk1.7.0_09\\include\" \n");
//			fw.write("	$(dllpro) --add-stdcall-alias main.o call_java.o -o student.dll \n");
//			fw.write("clean: \n");
//			fw.write("	del main.o call_java.o student.dll");
//			fw.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}		
	}
	
	private void templateWriteFile(String pathFile, String source) {
		File f = new File(pathFile);
		try {
			FileWriter fw = new FileWriter(f);
			fw.write(source);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	


	public void createJNIFile() {
		createJNIHeaderFile();
		createJNISourceFile();
		createMakefile();
	}

	public void compile() {		
		try {
			String [] cmd = {"mingw32-make","main"};
			Process p = Runtime.getRuntime().exec(cmd,null,new File("C:\\eclipse\\clab-plugin-dev\\eclipse\\clab"));
			InputStream ip = p.getErrorStream();
			InputStreamReader inputstreamreader = new InputStreamReader(ip);
			BufferedReader bufferedreader = new BufferedReader(
					inputstreamreader);
			String line = null;
			System.out.println(bufferedreader.readLine());
			while ((line = bufferedreader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setlLibraryPath(){
		String path = clabDir+";"+System.getProperty("java.library.path");
		System.out.println("***************** 1 "+path);
		System.setProperty("java.library.path", path);
		
	    //set sys_paths to null
	    Field sysPathsField;
		try {
			sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
			sysPathsField.setAccessible(true);
			sysPathsField.set(null, null);		
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public void callTest() {

	}

	
	public static void main(String[] args) {
//		try {
//			String [] cmd = {"mingw32-make","main"};
//			Process p = Runtime.getRuntime().exec(cmd,null,new File("C:\\eclipse\\clab-plugin-dev\\eclipse\\clab"));
//			InputStream ip = p.getErrorStream();
//			InputStreamReader inputstreamreader = new InputStreamReader(ip);
//			BufferedReader bufferedreader = new BufferedReader(
//					inputstreamreader);
//			String line = null;
//			System.out.println(bufferedreader.readLine());
//			while ((line = bufferedreader.readLine()) != null) {
//				System.out.println(line);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		
	}

}
